/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.ui;

import com.ivanbiz.dao.JurnalDAO;
import com.ivanbiz.dao.impl.JurnalDAOImpl;
import com.ivanbiz.model.Jurnal;
import com.ivanbiz.service.ServiceHelper;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;

/**
 *
 * @author Shbt Peterpan
 */
public class JurnalReportPanel extends javax.swing.JPanel {

    SimpleDateFormat simpleDateFormat;
    SimpleDateFormat simpleDateFormat1;
    JDialog jDialog;
    List<Jurnal> listJurnal;
    NumberFormat numberFormat;

    public JurnalReportPanel(JDialog jDialog, Date dari, Date sampai) {
        try {
            initComponents();
            numberFormat = NumberFormat.getCurrencyInstance(Locale.US);
            simpleDateFormat = new SimpleDateFormat("dd-MMMM-yyyy");
            jLabelJudul.setText("Dari tanggal " + simpleDateFormat.format(dari) + " sampai tanggal " + simpleDateFormat.format(sampai));
            JurnalDAO jurnalDAO = new JurnalDAOImpl();
            listJurnal = jurnalDAO.getData(Jurnal.class, dari, sampai);
            updateTableJurnal();
        } catch (Exception ex) {
            Logger.getLogger(JurnalReportPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public JurnalReportPanel(JDialog jDialog, String kos, Date dari, Date sampai) {
        try {
            initComponents();
            numberFormat = NumberFormat.getCurrencyInstance(Locale.US);
            simpleDateFormat = new SimpleDateFormat("dd-MMMM-yyyy");
            simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
            jLabelJudul.setText("Dari tanggal " + simpleDateFormat.format(dari) + " sampai tanggal " + simpleDateFormat.format(sampai));
            JurnalDAO jurnalDAO = new JurnalDAOImpl();
            listJurnal = jurnalDAO.getReportByKos(kos, simpleDateFormat1.format(dari), simpleDateFormat1.format(sampai));
            updateTableJurnal();
        } catch (Exception ex) {
            Logger.getLogger(JurnalReportPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jLabelJudul = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableJurnal = new javax.swing.JTable();

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel3.setText("Laporan Jurnal");

        jLabelJudul.setText("Dari Tanggal {} Sampai {}");

        tableJurnal.setAutoCreateRowSorter(true);
        tableJurnal.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tableJurnal);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(jLabelJudul, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelJudul)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabelJudul;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableJurnal;
    // End of variables declaration//GEN-END:variables

    private void updateTableJurnal() {
        String[] judul = {"No", "Date Reference", "Transaction Reference", "Accounting Reference", "Currency", "Debet", "Credit", "GL Account"};
        Object[][] isi = new Object[listJurnal.size()][8];
        int x = 0;
        int no = 0;
        String kode = "";
        for (Jurnal jurnal : listJurnal) {
            if (!kode.equals(jurnal.getTransactionReference())) {
                no += 1;
                kode = jurnal.getTransactionReference();
                isi[x][0] = no;
                isi[x][1] = simpleDateFormat.format(jurnal.getDateReference());
                isi[x][2] = kode;
                isi[x][3] = jurnal.getAccountingReference();
                isi[x][4] = jurnal.getCurrency();
            } else {
                isi[x][0] = "";
                isi[x][1] = "";
                isi[x][2] = "";
                isi[x][3] = "";
                isi[x][4] = "";
            }
            isi[x][5] = numberFormat.format(jurnal.getDebit());
            isi[x][6] = numberFormat.format(jurnal.getCredit());
            isi[x][7] = jurnal.getGLAccount();
            x++;
        }
        new ServiceHelper().setAutoRize(isi, judul, tableJurnal);
    }
}
